package com.tbm.console.proj.service.impl;

import com.tbm.console.proj.entity.ChartShowInfoEntity;
import com.tbm.console.proj.repository.ChartShowInfoRepository;
import com.tbm.console.proj.service.ChartShowInfoService;
import com.tbm.console.util.PageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


//说明 字符串：，作为替换字符串 LOWERChartShowInfo                ChartShowInfo

@Service
public class ChartShowInfoServiceImpl implements ChartShowInfoService {

    @Autowired
    private ChartShowInfoRepository chartShowInfoRepository;

    @Override
    public List<ChartShowInfoEntity> findAll() {
        return findAll(new PageData());
    }

    @Override
    public List<ChartShowInfoEntity> findAll(Integer start, Integer length) {
        return findAll(new PageData(),start,length);
    }

    @Override
    public List<ChartShowInfoEntity> findAll(PageData pageData) {
        Specification<ChartShowInfoEntity> sp = this.getChartShowInfoSpecification(pageData);
        return chartShowInfoRepository.findAll(sp);
    }

    @Override
    public List<ChartShowInfoEntity> findAll(PageData pageData, Integer start, Integer length) {
        Specification<ChartShowInfoEntity> sp = this.getChartShowInfoSpecification(pageData);
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        Pageable pageable = new PageRequest(start, length, sort);
        return chartShowInfoRepository.findAll(sp,pageable).getContent();
    }

    @Override
    public long count() {
        return count(new PageData());
    }

    @Override
    public long count(ChartShowInfoEntity chartShowInfo) {
        return chartShowInfoRepository.count(Example.of(chartShowInfo));
    }

    @Override
    public long count(PageData pageData) {
        Specification<ChartShowInfoEntity> sp = this.getChartShowInfoSpecification(pageData);
        return chartShowInfoRepository.count(sp);
    }

    @Override
    public boolean deleteById(Integer id) {
        chartShowInfoRepository.deleteById(id);
        return false;
    }

    @Override
    public boolean deleteByIds(Iterable<ChartShowInfoEntity> iterable) {
        return false;
    }

    @Override
    public ChartShowInfoEntity save(ChartShowInfoEntity chartShowInfo) {
        List<ChartShowInfoEntity> list= saveAll(Arrays.asList(chartShowInfo));
        return list.size()==0?new ChartShowInfoEntity():list.get(0);
    }

    @Override
    public List<ChartShowInfoEntity> saveAll(List<ChartShowInfoEntity> chartShowInfos) {
        return chartShowInfoRepository.saveAll(chartShowInfos);
    }

    @Override
    public ChartShowInfoEntity findById(Integer id) {
        ChartShowInfoEntity chartShowInfo = new ChartShowInfoEntity();
        try{
            chartShowInfo = chartShowInfoRepository.findById(id).get();
        }catch (Exception e){

        }
        return chartShowInfo;
    }

    @Override
    public List<ChartShowInfoEntity> findAllById(Integer[] ids) {
        return chartShowInfoRepository.findAllById(Arrays.asList(ids));
    }

    //通过Specification来获取自定义的查询条件
    private Specification<ChartShowInfoEntity> getChartShowInfoSpecification(PageData pageData){
        return new Specification() {
            @Override
            public javax.persistence.criteria.Predicate toPredicate(Root root, CriteriaQuery criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> preList = new ArrayList<Predicate>();
                if (null!=pageData.get("deleteFlag")) {
                    preList.add(criteriaBuilder.equal(root.get("deleteFlag"), pageData.get("deleteFlag").toString()));
                }
                
                Predicate[] p = new Predicate[preList.size()];
                return criteriaBuilder.and(preList.toArray(p));
            }
        };
    }
}